package com.mall.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.mall.entity.sys.ProdtypeEO;
import com.mall.entity.sys.ProductEO;
import com.mall.entity.sys.ResEO;
import com.mall.exception.MsgException;
import com.mall.service.sys.IProdtypeService;
import com.mall.service.sys.IProductService;
import com.mall.utils.FTPTool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;





/**
 * <p>
 * 商品表  前端控制器
 * </p>
 *
 * @author Dczzs
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/sys/prod")
public class ProductController {
	
	
	@Autowired
	private FTPTool ftpTool;


	
	@Autowired
	private IProductService service;
	@Autowired
	private IProdtypeService proptypeService;
	
	
	
	
	@GetMapping("/list")
	public ResEO list() {
		ResEO res = new ResEO();
		
		List<ProductEO> list = service.list();
		res.setData(list);
		return res;
		
	}
	
	
	
	
	@GetMapping("/list_by_typeid/{id}")
	public  ResEO listByType(@PathVariable Integer id) {
		ResEO res = new ResEO();
		
		QueryWrapper<ProductEO> qw = new QueryWrapper<>();
		qw.eq("type_id", id);
		 List<ProductEO> list = service.list(qw);	
		res.setData(list); 
		
		return res;
		
	}
	
	
	@GetMapping("/list_proptype")
	public ResEO listproptype() {
		ResEO res = new ResEO();
		
		List<ProdtypeEO> list = proptypeService.list();
		res.setData(list);
		
		
		return res;
		
	}
	
	
	@ApiOperation(value = "编辑用户",notes="根据用户ID查询当前用户完整信息")
	@ApiImplicitParam(paramType="path",name="id",value="用户ID",required=true)
	@GetMapping("/edit/{id}")
	public ResEO edit( @PathVariable Integer id) {
		ResEO res = new ResEO();
		
		try {
			ProductEO eo =  service.queryById(id);
			res.setData(eo); 
		} catch (Exception e) {
			e.printStackTrace();
			res.setStatus(-1); 
		}
		return res;
		
	}
	
	@PostMapping("/save")
	public ResEO save(ProductEO eo,//
					  MultipartFile imgUrlFile1, MultipartFile imgUrlFile2, MultipartFile imgUrlFile3, MultipartFile imgUrlFile4, MultipartFile imgUrlFile5 ){
	ResEO res = new ResEO();
	
	
	try {
		eo.setImgUrl1(uploadFile(imgUrlFile1,eo.getImgUrl1()));
		eo.setImgUrl2(uploadFile(imgUrlFile2,eo.getImgUrl2()));
		eo.setImgUrl3(uploadFile(imgUrlFile3,eo.getImgUrl3()));
		eo.setImgUrl4(uploadFile(imgUrlFile4,eo.getImgUrl4()));
		eo.setImgUrl5(uploadFile(imgUrlFile5,eo.getImgUrl5()));
	} catch (Exception e) {
		e.printStackTrace();
		res.appendMsg(e.getMessage());
	}
		try {
			boolean saveOrUpdate = service.saveOrUpdate(eo);
			if(saveOrUpdate) {
				res.setMsg("已保存");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			res.setStatus(-1); 
		}
		return res;
	}
	
	private String uploadFile(MultipartFile imgUrlFile,String imgUrl) {
		try {
		if(imgUrlFile!=null ) { 
			ftpTool.connect();
				// 创建文件上传核心类 
				if(Strings.isNullOrEmpty(imgUrl)) {
					imgUrl = "/" + System.nanoTime() + ".jpg";	
				}else {
					try {
						ftpTool.deleteFile(imgUrl);
					} catch (Exception e) {
						System.out.println(e.getMessage());
					}
				}

				
					ftpTool.uploadFileFromIS(imgUrl, imgUrlFile.getInputStream());
					System.out.println(String.format("Name = %s; Value = %s", "imgUrl",imgUrl));
					return imgUrl;
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw new MsgException("附件保存失败，请编辑后重新上传，或者联系管理员&*&*&*&*&*&");
	}
		return imgUrl;
	
}
	
	@GetMapping("/delete/{id}")
	public ResEO delete(@PathVariable Integer id) {
		ResEO res = new ResEO();
		
		try {
			service.delete(id);
		} catch (MsgException e) {
			e.printStackTrace();
			res.setStatus(1);
		} catch (Exception e) {
			e.printStackTrace();
			res.setStatus(-1);
		}
		
		return res;
	}
	
	
	
	@GetMapping("/add")
	public ResEO add(ProductEO eo) {
		ResEO res = new ResEO();
		
		
		try {
			eo.setStatus(0);
			eo.setCreatedBy(1);
			res.setData(eo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return res;
	}
	

}
